package binarysearchtree;

/**
 * @ClassName BinarySearchTree
 * @Description 二叉搜索树学习
 * @Author ZJX
 * @Date 2024/8/5 22:01
 * @Version 1.0
 */

public class BinarySearchTree {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;

    /*
     * @param key:
      * @return boolean
     * @author: ZJX
     * @description: 二叉搜索树 搜索关键值
     *      二叉搜索树的搜索操作在平衡树情况下的时间复杂度为 log N
     *                   单分支的树为 O (N)
     * @date: 2024/8/5 22:29
     */
    public boolean search(int key){
        TreeNode cur = root;
        while(cur != null){
            if (cur.val == key){
                return true;
            } else if (cur.val < key) {
                cur = cur.right;
            } else {
                cur = cur.left;
            }
        }
        return false;
    }

    /*
     * @param key:
      * @return void
     * @author: ZJX
     * @description:  二叉搜索树 插入操作
     * @date: 2024/8/5 22:51
     */
    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);

        if (root == null){
            root = node;
            return true;
        }

        TreeNode cur = root;
        TreeNode parent = null;

//        找到插入点
        while (cur != null){
            parent = cur;
            if (cur.val == key){
                return false; // 二叉搜索树不能有一样的元素
            } else if (cur.val < key) {
                cur = cur.right;
            } else {
                cur = cur.left;
            }
        }

//        插入新节点
        if (parent.val > key){
            parent.left = node;
        } else {
            parent.right = node; //不会出现相等的情况
        }

        return true;
    }

    /*
     * @param key:
      * @return boolean
     * @author: ZJX
     * @description: 递归法 插入二叉树搜索树
     * @date: 2024/8/5 23:36
     */
    public boolean insertRecursive(int key) {
        if (root == null){
            root = new TreeNode(key);
            return true;
        } else {
            return insertRecursiveHelper(root,key);
        }
    }

    private boolean insertRecursiveHelper(TreeNode node, int key) {
        if (node.val == key){
            return false; // 二叉搜索树种不能有一样的元素
        }

        // 如果插入值小于当前节点值，进入左子树
        if (key < node.val){
            if (node.left == null){
                node.left = new TreeNode(key);
                return true;
            } else {
                // 递归调用左子树
               return insertRecursiveHelper(node.left,key);
            }
        }

//        走到此处说明 key 大于 node.val
        if (node.right == null){
            node.right = new TreeNode(key);
            return true;
        } else {
            // 递归调用右子树
            return insertRecursiveHelper(node.right,key);
        }
    }

    /*
     * @param key:
      * @return boolean
     * @author: ZJX
     * @description: 二叉搜索树的 删除操作
     * @date: 2024/8/6 13:00
     */
    public boolean delete(int key) {
        // 初始化待删除节点和其父节点
        TreeNode cur = root;
        TreeNode parent = null;

        // 查找待删除的节点
        while(cur != null && cur.val != key){
            parent = cur;
            if (cur.val < key){
                cur = cur.right;
            }
            if (cur.val > key){
                cur = cur.left;
            }
        }

        // 没有找到待删除的节点
        if (cur == null){
            return false;
        }

//        找到了节点 调用辅助方法分情况进行删除
        return deleteNode(cur, parent);
    }

    private boolean deleteNode(TreeNode cur, TreeNode parent) {
        if (cur.left == null){
//            1. 待删除节点没有左子树
            if (cur == root){
               root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            } else if (cur == parent.right){
//                只有以上三种情况
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
//           2. 待删除节点没有右子树
            if (cur == root){
                root = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            } else if (cur == parent.right) {
                parent.right = cur.left;
            }

        } else {
//            3. 待删除节点有左右子树

            // 寻找右子树中最左边的节点（后继节点）

            TreeNode target = cur.right;
            TreeNode targetParent = cur;

            while(target.left != null){
                targetParent = target;
                target = target.left;
            }

//            走出循环 说明已经找到了后继节点 用后继节点的值替换当前节点的值
            cur.val = target.val;

//            删除后继节点
            if (targetParent.left == target) {
                targetParent.left = target.right;
            } else {
                targetParent.right = target.right; //这是单分支的情况下
            }
        }
        return true;
    }
}
